#include "includes.h"
#include "structs.h"
#include "declares.h"

void add_step_time(igraph_vector_ptr_t *p, struct step_timer_t *s)
{
        igraph_vector_ptr_push_back(p, s);
}

struct step_timer_t *make_timer(double synct, double evalt)
{
        struct step_timer_t *st=xmalloc(sizeof(struct step_timer_t));
        st->synct=synct;
        st->evalt=evalt;
        return st;
}

void reset_step_timer(igraph_vector_ptr_t *p)
{
        igraph_vector_ptr_clear(p);
}


double get_current_time() {
	struct 	timeval t;
	gettimeofday(&t, 0);
	return (double) t.tv_sec + (double) t.tv_usec / 1000000;
}

/* const int N_Timers = 7; //currently, 6 timers are available */

static double _timers[N_Timers]; // timers
static double _acc_time[N_Timers]; // accumulated time

/* call this before timing anything */
void init_timers() {
	for (int i = 0; i < N_Timers; i++) {
		_acc_time[i] = 0;
	}
}


//currently, only 6 timers are used, others can be defined by users
/* code betweeen start_timer and stop_timer will be timed , call get_timer to have the result */
void start_timer(int i) {
	_timers[i] = get_current_time();
}

/* call this before retiming a process */
void reset_timer(int i) {
	_timers[i] = get_current_time();
	_acc_time[i] = 0;
}

void stop_timer(int i) {
	double t = get_current_time();
	_acc_time[i] += t - _timers[i];
}

double get_timer(int i) {
	return _acc_time[i];
}
